package your.rfgui.application.data;

import eu.webhib.frmwrk.service.utils.RangeData;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import your.rfgui.extgui.service.intf.GuiParentChildService;

public class MyParentChildService implements GuiParentChildService<MyParentChildEntity> {

   private static final Logger log = Logger.getLogger(MyParentChildService.class);
   private List<MyParentChildEntity> totalList = new ArrayList<MyParentChildEntity>();
   private List<MyParentChildEntity> rootList = null;
   private static MyParentChildService instance = new MyParentChildService();

   private MyParentChildService() {
      log.info("MyParentChildService");
      for (int i = 0; i < 4; i++) {
         MyParentChildEntity parent = new MyParentChildEntity("Directory " + i);
         totalList.add(parent);
         for (int j = 0; j < 6; j++) {
            MyParentChildEntity child = new MyParentChildEntity("Directory " + i + " Label " + j);
            child.setParent(parent);
            totalList.add(child);
         }
      }
   }

   public static MyParentChildService getInstance() {
      return instance;
   }

   public List<MyParentChildEntity> getRootList() {
      log.info("getRootList");

      if (rootList == null) {
         rootList = new ArrayList<MyParentChildEntity>();
         for (MyParentChildEntity item : totalList) {
            if (item.getParent() == null) {
               rootList.add(item);
            }
         }
      }
      return rootList;
   }

   public MyParentChildEntity createNew() {
      return new MyParentChildEntity();
   }

   public void save(MyParentChildEntity entity) {
      totalList.add(entity);
   }

   public MyParentChildEntity update(MyParentChildEntity entity) {
      log.info("update");
      rootList = null;
      return entity;
   }

   public void updateFromCopy(MyParentChildEntity entityToChange, MyParentChildEntity changedEntity) {
      throw new UnsupportedOperationException("Not supported yet.");
   }

   /* todo: remove */
   public void moveToNewParent(MyParentChildEntity entityToChange, MyParentChildEntity newParent) {
      throw new UnsupportedOperationException("Not supported yet.");
   }

   public void delete(MyParentChildEntity entity) {
      totalList.remove(entity);
      if (entity.getParent() != null) {
         entity.getParent().getChildren().remove(entity);
      } else {
         rootList = null;
      }
   }

   public void deleteAll() {
      throw new UnsupportedOperationException("Not supported yet.");
   }

   public MyParentChildEntity findId(Long id) {
      throw new UnsupportedOperationException("Not supported yet.");
   }

   public List<MyParentChildEntity> findByNamedQuerySuffix(String namedQueryName, Object... paramValue) {
      throw new UnsupportedOperationException("Not supported yet.");
   }

   public List<MyParentChildEntity> findByAttribute(String attrName, Object attrValue) {
      throw new UnsupportedOperationException("Not supported yet.");
   }

   public MyParentChildEntity findByAttributeSingle(String attrName, Object attrValue) {
      throw new UnsupportedOperationException("Not supported yet.");
   }

   public List<MyParentChildEntity> findAll() {
      throw new UnsupportedOperationException("Not supported yet.");
   }

   public RangeData<MyParentChildEntity> findAllRange(int startIndex, int size) {
      throw new UnsupportedOperationException("Not supported yet.");
   }

   public int count() {
      throw new UnsupportedOperationException("Not supported yet.");
   }
}
